<?php

namespace app\common\model\Business;

use app\common\model\Region;
use think\Model;
use traits\model\SoftDelete;

class Business extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'business';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'avatar_text',
        'gender_text',
        'province_text',
        'city_text',
        'district_text',
        'poster_text',
        'date',
        'collections',
        'deal_text',
        'auth_text',
        'region'
    ];
    public function getAvatarTextAttr($value,$data)
    {
        if(!isset($data['avatar']))return;
        $defaultImg = config('site.defaultImg')?config('site.defaultImg'):'/uploads/default.jpg';
        return is_file(ROOT_PATH.'public'.$data['avatar'])?$data['avatar']:$defaultImg;
    }

    public function getProvinceTextAttr($value,$data)
    {
        return isset($data['province'])?model('region')->where('code',$data['province'])->find()['name']:'';
    }
    public function getCityTextAttr($value,$data)
    {
        return isset($data['city'])?model('region')->where('code',$data['city'])->find()['name']:'';
    }
    public function getDistrictTextAttr($value,$data)
    {
        return isset($data['district'])?model('region')->where('code',$data['district'])->find()['name']:'';
    }
    public function getRegionAttr($value,$data)
    {
        if(!isset($data['province']))return;
        ($region = $data['district']) || ($region = $data['city']) || ($region = $data['province']);
        return $region?$region:'';
    }
    public function getPosterTextAttr($value,$data)
    {
        // $defaultImg = config('site.poster')?config('site.poster'):'/assets/home/images/poster-back.png';
        if(!isset($data['poster']))return;
        return is_file(ROOT_PATH.'public'.$data['poster'])?$data['poster']:'';
    }
    public function getDateAttr($value,$data){
        if(!isset($data['createtime']))return;
        return date('Y-m-d',$data['createtime']);
    }
    public function getCollectionsAttr($value,$data)
    {
        // $collections = Collection::where('busid',$data['id'])->select()->toArray();
        // return $collections;
        return $this->hasMany('collection','busid');
    }
        
    public function getGenderList()
    {
        return ['0' => __('Secrecy'), '1' => __('Male'), '2' => __('FeMale')];
    }

    public function getDealList()
    {
        return ['0' => __('Deal 0'), '1' => __('Deal 1')];
    }

    public function getAuthList()
    {
        return ['0' => __('Auth 0'), '1' => __('Auth 1')];
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDealTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deal']) ? $data['deal'] : '');
        $list = $this->getDealList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAuthTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth']) ? $data['auth'] : '');
        $list = $this->getAuthList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function source()
    {
        return $this->belongsTo(Source::class,'sourceid','id',[],'left')->setEagerlyType(0);
    }
    public function getSourceList()
    {
        return Source::field('id,name')->where('deletetime','null')->select();
    }
}
